<?php

Class Au_suburb_geo_model extends CI_Model {

    public function getAuSuburbGeo($part) {
//    $data1 = explode(',',$address);
//
//    $pcode=$data1[1];
//
//    $data2 = explode(' ',$data1[0]);
//    $x=count($data2);
//    $state=$data2[$x-1];
//    $locality=str_replace($state,'',$data1[0]);
//    $this->db->where('Pcode',$pcode);
//    $this->db->where('Locality',$locality);
//    $this->db->where('State1',$state);
//    $query = $this->db->get('au_suburb_geo');
//    return $query->result_array();
        //$result = mysql_query("select * from au_suburb_geo where Locality like '%$q%' or Pcode like '%$q%' or State1 like '%$q%' order by Pcode LIMIT 25");
        $this->db->like('Locality', $part);
        $this->db->or_like('Pcode', $part);
        $this->db->or_like('State1', $part);
        $this->db->order_by('Pcode');
        $this->db->limit(25);
        $query = $this->db->get('au_suburb_geo');
        return $query->result_array();
    }
    
   public function loadSuburbGeo($data = array(), $sort = array()) {
        $this->db->select("*");
        $this->db->from("au_suburb_geo");

        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $this->db->where($key, $value);
            }
        }

        if (!empty($sort)) {
            foreach ($sort as $key => $value) {
                $this->db->order_by($key, $value);
            }
        }
        $query = $this->db->get();

        return $query->result();
    }

    public function getSuburbGeoById($id) {
        $result = $this->loadSuburbGeo(array('ID' => $id));
        if (!empty($result))
            return $result[0];
        return FALSE;
    }
    public function getSuburbById($id){
        $this->db->select('*');
        $this->db->from('franchise_suburbs,franchise,au_suburb_geo');
        $this->db->where('franchise_suburbs.franchise_id = franchise.id');
        $this->db->where('au_suburb_geo.ID = franchise_suburbs.suburbs_id');
        $this->db->where('au_suburb_geo.ID',$id);
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->result();
        if (!empty($result))
            return $result[0];
        return FALSE;
    }
    public function getAuSuburbById($id){
        $this->db->select('*');
        $this->db->from('au_suburb_geo');
        $this->db->where('ID',$id);
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->result();
        if (!empty($result))
            return $result[0];
        return FALSE;
    }
    
    public function getSuburbAround($lat, $lon){
         $this->db->select("*, (((acos(sin((" . $lat . "*pi()/180)) *
                sin((`lat`*pi()/180))+cos((" . $lat . "*pi()/180)) *
                cos((`lat`*pi()/180)) * cos(((" . $lon . " - `long`)*
                pi()/180))))*180/pi())*60*1.1515
            ) as distance");
         $this->db->from("au_suburb_geo");
         $this->db->where("(((acos(sin((" . $lat . "*pi()/180)) *
                sin((`lat`*pi()/180))+cos((" . $lat . "*pi()/180)) *
                cos((`lat`*pi()/180)) * cos(((" . $lon . " - `long`)*
                pi()/180))))*180/pi())*60*1.1515
            ) <= 2");
         $this->db->order_by('distance', 'ASC');
         $query = $this->db->get();
         return $query->result_array();
    }

}

?>
